package mashuaiproject.mashuaiproject.GoEasyTest;

import com.alibaba.fastjson.JSON;
import io.goeasy.GoEasy;
import mashuaiproject.mashuaiproject.dto.AddFriendsUser;
import mashuaiproject.mashuaiproject.dto.OnlieUserMesDto;
import mashuaiproject.mashuaiproject.mapper.OnlieCoummtiyMapper;
import mashuaiproject.mashuaiproject.mapper.UserMapper;
import mashuaiproject.mashuaiproject.model.AddFriend;
import mashuaiproject.mashuaiproject.model.OnlineCommuntiy;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.JMontiorSerivce;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoeasyTestContrller {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OnlieCoummtiyMapper onlieCoummtiyMapper;
    @Autowired
    private JMontiorSerivce jMontiorSerivce;
//    @Scheduled(fixedRate = 2000)
    //进入页面就显示的聊天记录
    @ResponseBody
    @GetMapping("/showCommuntiy")
    public List<OnlieUserMesDto> showCommuntiys(@RequestParam("staues")String staues,HttpServletRequest request,
                                                @RequestParam("friednToken")String friednToken){
        System.err.println("接受的参数："+friednToken+"--"+staues);
        Users cruuent_users= (Users) request.getSession().getAttribute("user");
        List<OnlieUserMesDto> onlieUserMesDtos=new ArrayList<>();
        if(staues.equals("siliao")){
            if(friednToken=="" || friednToken==null){
                System.err.println("1");
            }else {
               //查找和当用户的聊天记录
                Users users=userMapper.findByToken(friednToken);
                //如果当前用户
                List<OnlineCommuntiy> onlineCommuntiy=onlieCoummtiyMapper.selectOneCommuntiy(cruuent_users.getId(),users.getId());
                if(onlineCommuntiy.size()!=0){
                    for(OnlineCommuntiy onlineCommuntiy1:onlineCommuntiy){
                        if(true){
                            Users users1=userMapper.findByUser(onlineCommuntiy1.getUserId());
                            OnlieUserMesDto onlieUserMesDto=new OnlieUserMesDto();
                            onlieUserMesDto.setUsers(users1);
                            BeanUtils.copyProperties(onlineCommuntiy1,onlieUserMesDto);
                            onlieUserMesDtos.add(onlieUserMesDto);
                        }

                    }

                }
            }
        }


            //删除后，安装时间升序查询
        if(staues.equals("qunliao")){
            //先判断，只保留后一百条聊天记录
            List<OnlineCommuntiy> onlineCommuntiy3=onlieCoummtiyMapper.selectAllCommuntiy3();
            //每次进入页面都先删除删除后面一百条信息
            for(int i=0;i<onlineCommuntiy3.size();i++){
                if(i>99){

                    //删除后一百条群里信息
                    int delMes=onlieCoummtiyMapper.delYiBaiBefroe(onlineCommuntiy3.get(i).getId());
                }
            }
            List<OnlineCommuntiy> onlineCommuntiy=onlieCoummtiyMapper.selectAllCommuntiy();
            //对每个评论的userI绑定信息
            for(OnlineCommuntiy onlineCommuntiy1:onlineCommuntiy){
                OnlieUserMesDto onlieUserMesDto=new OnlieUserMesDto();
                Users users=userMapper.findByUser(onlineCommuntiy1.getUserId());
                BeanUtils.copyProperties(onlineCommuntiy1,onlieUserMesDto);
                onlieUserMesDto.setUsers(users);
                onlieUserMesDtos.add(onlieUserMesDto);
            }
        }

        return onlieUserMesDtos;
    }

    @ResponseBody
    @GetMapping("/webCommuntiy")
    public String GoEasy(@RequestParam("content")String  content,
                         @RequestParam("str1") String str1,
                         HttpServletRequest request,
                         @RequestParam("friednToken")String friednToken) throws IOException {
        //获取当前用户的id
        Users users= (Users) request.getSession().getAttribute("user");
        OnlineCommuntiy onlineCommuntiy=new OnlineCommuntiy();
        onlineCommuntiy.setUserId(users.getId());
        onlineCommuntiy.setContent(content);
        onlineCommuntiy.setGmt_create(System.currentTimeMillis());
        onlineCommuntiy.setSite("安徽滁州");//默认地址
        //将判断是群聊还是私聊
      if(str1.equals("qunliao")){
            //将信息插入群聊数据
          System.err.println("群里聊天");
           onlineCommuntiy.setWithUserId(0);
          onlieCoummtiyMapper.insetQunLiao(onlineCommuntiy);

        }else if(str1.equals("siliao")){
            //在天津一个withUserId属性值
            //获取私聊的用户id
          Users users1=userMapper.findByToken(friednToken);
          onlineCommuntiy.setWithUserId(users1.getId());
          onlieCoummtiyMapper.insetQunLiao(onlineCommuntiy);
          //再插入个饭序列
          onlineCommuntiy.setUserId(users1.getId());
          onlineCommuntiy.setWithUserId(users.getId());
          onlieCoummtiyMapper.insetQunLiao(onlineCommuntiy);
        }

        //封装发送人的信息
        OnlieUserMesDto onlieUserMesDto=new OnlieUserMesDto();
        BeanUtils.copyProperties(onlineCommuntiy,onlieUserMesDto);
        onlieUserMesDto.setUsers(users);
        String str = JSON.toJSONString(onlieUserMesDto); // List转json
        GoEasy goEasy = new GoEasy("rest-hangzhou.goeasy.io", "BC-a08e601904a34f429a0241e959bef29a");
        goEasy.publish("demo",str);


        return "webscorttext";
    }

    //加好友
    @ResponseBody
    @GetMapping("/addFriends")
    public Map<String ,Object> addFriendss(@RequestParam("frienId")Integer friendId,HttpServletRequest request){
        Map<String ,Object> map=new HashMap<>();
        Users users= (Users) request.getSession().getAttribute("user");
        //判断有没有已经存在这个用户
        Integer isHasShenqing=onlieCoummtiyMapper.checkIsShenqing(users.getId(),friendId);
        if(isHasShenqing!=0){
            map.put("satues","yes");
            return  map;
        }
        //将加好友信息插入数据库（申请人--->被申请人）
        AddFriend addFriend=new AddFriend();
        addFriend.setUserId(users.getId());
        addFriend.setFriendId(friendId);
        addFriend.setGmt_create(System.currentTimeMillis());
        addFriend.setIsAgree(0);
        System.err.println(addFriend.toString());
        Integer insert=onlieCoummtiyMapper.insertAddFriend(addFriend);
        ///插入（被申请人--申请人）
        addFriend.setUserId(friendId);
        addFriend.setFriendId(users.getId());
        Integer insert2=onlieCoummtiyMapper.insertAddFriend(addFriend);
        //---------插入加好友动态
        jMontiorSerivce.insertDongtai(users.getId(),9,friendId);
        jMontiorSerivce.insertDongtai(friendId,10,users.getId());
        map.put("satues","no");

        return map;
    }

    //对好友请求的处理
    @ResponseBody
    @GetMapping("/isAgree")
    public  String isAgrees(@RequestParam("frienId")Integer frienId,
                            @RequestParam("chioce")String chioce,
                            HttpServletRequest request){
        Users users= (Users) request.getSession().getAttribute("user");

        if (chioce.equals("yes")){
            //同意好友SS
            System.err.println(frienId+"--"+users.getId());
            int updateIsAgree=onlieCoummtiyMapper.updateFriedn(frienId,users.getId(),1);
            int updateIsAgree2=onlieCoummtiyMapper.updateFriedn(users.getId(),frienId,1);
            jMontiorSerivce.insertDongtai(frienId,11,users.getId());
        }else if(chioce.equals("no")){
            //不同意.删除申请记录
            Integer del=onlieCoummtiyMapper.delShenqing(frienId,users.getId());
            Integer del2=onlieCoummtiyMapper.delShenqing(users.getId(),frienId);
            jMontiorSerivce.insertDongtai(frienId,12,users.getId());

        }
        return null;
    }
    //进入聊天时
    @GetMapping("/webscort")
    public  String  text66(Model model,HttpServletRequest request,
                           @RequestParam(value = "chioce",required = false)String  chioce){
        Users users= (Users) request.getSession().getAttribute("user");
        System.err.println("当前用户的好："+chioce);
        //携带所有已经存在的会员
        List<Users> usersList=userMapper.selecAllUser4();

        //查找出又多少会员
        Integer userConut=userMapper.selectCountUsers();
        //找出当前用户的好友类表
        List<AddFriend> addFriendList=onlieCoummtiyMapper.selectOneFried(users.getId());
        List<AddFriendsUser> addFriendsUserList=new ArrayList<>();
        for(AddFriend addFriend:addFriendList){
            Users users1=userMapper.findByUser(addFriend.getFriendId());
            AddFriendsUser addFriendsUser=new AddFriendsUser();
            addFriendsUser.setAddFriend(addFriend);
            addFriendsUser.setUsers(users1);
            addFriendsUserList.add(addFriendsUser);
        }

        //如果不是群聊
        //1.现货去点击私聊的用户
        model.addAttribute("friednToken",chioce);
        model.addAttribute("addFriendsUserList",addFriendsUserList);
        model.addAttribute("countUsers",userConut);
        model.addAttribute("usersList",usersList);
        if(chioce!=null){
            model.addAttribute("staues","siliao");
            Users users1=userMapper.findByToken(chioce);
            model.addAttribute("communtiyTitle",users1.getName());

        }else {
            model.addAttribute("staues","qunliao");
            model.addAttribute("communtiyTitle","交流社区");
        }


        return "webscorttext";
    }
}
